package com.itheima.admin.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.admin.dto.AdChannelPageRequestDto;
import com.itheima.admin.mapper.AdChannelMapper;
import com.itheima.admin.pojo.AdChannel;
import com.itheima.admin.service.AdChannelService;
import com.itheima.common.constants.SystemConstants;
import com.itheima.common.exception.LeadNewsException;
import com.itheima.common.vo.PageResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {


    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 分页查询频道信息
     *
     * @param dto
     * @return
     */
    @Override
    public PageResultVo pageList(AdChannelPageRequestDto dto) {
        //创建分页查询对象
        IPage<AdChannel> iPage = new Page<>(dto.getPage(), dto.getSize());
        //构建条件
        LambdaQueryWrapper<AdChannel> queryWrapper = new LambdaQueryWrapper<>();
        //频道名称模糊查询
        queryWrapper.like(StringUtils.isNotEmpty(dto.getName()), AdChannel::getName, dto.getName());
        //状态精确查询
        queryWrapper.eq(null != dto.getStatus(), AdChannel::getStatus, dto.getStatus());
        //执行并组装返回
        //IPage<AdChannel> page = page(iPage, queryWrapper);
        page(iPage, queryWrapper);
        log.info("分页查询频道成功");

        //return PageResultVo.pageResult(page.getPages(), page.getSize(), page.getTotal(), page.getRecords());
        return PageResultVo.pageResult(dto.getPage(),dto.getSize(),iPage.getTotal(),iPage.getRecords());
    }

    /**
     * 新增频道
     *
     * @param adChannel
     * @return
     */
    @Override
    public Boolean add(AdChannel adChannel) {

        if (adChannel.getName().length()>10) {
        throw  new LeadNewsException("频道名称不能超过10位");
        }
        //先判断key存不存在
        Boolean key = stringRedisTemplate.hasKey(SystemConstants.REDISKEY_CHANNELLIST);

        //如果key存在先删除redis缓存
        if (key) {
            stringRedisTemplate.delete(SystemConstants.REDISKEY_CHANNELLIST);
        }
        //补充数据--是否默认asDefault和创建时间
        adChannel.setAsDefault(false);
        adChannel.setCreatedTime(new Date());
        //执行新增操作
        boolean save = save(adChannel);

        if (save){//将查询结果存入redis中
            List<AdChannel> list = list();
            stringRedisTemplate.opsForValue().set(SystemConstants.REDISKEY_CHANNELLIST, JSON.toJSONString(list));
        }
        log.info("新增频道成功");
        return save;
    }
}
